package sat.component;


import org.mt4j.MTApplication;
import org.mt4j.components.visibleComponents.shapes.MTRectangle;
import org.mt4j.components.visibleComponents.shapes.MTRoundRectangle;
import org.mt4j.input.inputProcessors.IGestureEventListener;
import org.mt4j.input.inputProcessors.MTGestureEvent;
import org.mt4j.input.inputProcessors.componentProcessors.dragProcessor.DragEvent;
import org.mt4j.input.inputProcessors.componentProcessors.dragProcessor.DragProcessor;
import org.mt4j.util.MTColor;
import org.mt4j.util.math.Vector3D;

import sat.component.DragAndDropButton.ButtonType;
import sat.model.UseCaseModel;

public class UseCaseComponentPopUp extends UseCasePopUp{

	private MTRectangle nameField;

	public static final float nameFieldHeight = 30;

	private KeyboardTextField contentText;

	private UseCaseModel model;

	private Vector3D pos = null;

	private UseCaseComponentPopUp instance;

	public void clearAll(){
		contentText.setText("");
	}
	
	public void highLightValid(){
		//this.setStrokeColor(new MTColor(202,94,47));
		this.setStrokeColor(MTColor.GREEN);
		instance.setStrokeWeight(10);
	}
	
	public void highLightInvalid(){
		//this.setStrokeColor(new MTColor(202,94,47));
		this.setStrokeColor(MTColor.RED);
		instance.setStrokeWeight(10);
	}

	public void lowLight(){
		instance.setStrokeColor(new MTColor(56, 62, 52, 255));
		this.setStrokeWeight(1);
	}

	public void addCornerButtonListener(final MTRoundRectangle button,final ButtonType t){
		button.addGestureListener(DragProcessor.class,
				new IGestureEventListener() {
			public boolean processGestureEvent(MTGestureEvent evt) {
				DragEvent de = (DragEvent) evt;
				switch (de.getId()) {
				case MTGestureEvent.GESTURE_STARTED:
					pos = new Vector3D(button.getCenterPointRelativeToParent());
					if(t == ButtonType.SAVE){
						clearButton.setPickable(false);
						saveAndQuitButton.setPickable(false);
						quitButton.setPickable(false);
					}
					else if(t == ButtonType.CLEAR){
						saveButton.setPickable(false);
						saveAndQuitButton.setPickable(false);
						quitButton.setPickable(false);
					}
					else if(t == ButtonType.SAVEANDQUIT){
						saveButton.setPickable(false);
						clearButton.setPickable(false);
						quitButton.setPickable(false);
					}
					else if(t == ButtonType.QUIT){
						saveButton.setPickable(false);
						clearButton.setPickable(false);
						saveAndQuitButton.setPickable(false);
					}
					highLightInvalid();
					break;
				case MTGestureEvent.GESTURE_UPDATED:
					if(instance.isGeometryContainsPointLocal(button.getCenterPointRelativeToParent())){
						highLightValid();
					}
					else{
						highLightInvalid();
					}
					break;
				case MTGestureEvent.GESTURE_ENDED:
					if(t == ButtonType.SAVE){
						clearButton.setPickable(true);
						saveAndQuitButton.setPickable(true);
						quitButton.setPickable(true);
					}
					else if(t == ButtonType.CLEAR){
						saveButton.setPickable(true);
						saveAndQuitButton.setPickable(true);
						quitButton.setPickable(true);
					}
					else if(t == ButtonType.SAVEANDQUIT){
						saveButton.setPickable(true);
						clearButton.setPickable(true);
						quitButton.setPickable(true);
					}
					else if(t == ButtonType.QUIT){
						saveButton.setPickable(true);
						clearButton.setPickable(true);
						saveAndQuitButton.setPickable(true);
					}
					
					if(instance.isGeometryContainsPointLocal(button.getCenterPointRelativeToParent()))
					{
						System.out.println("ending");
						//TODO		
						if(t == ButtonType.SAVE){
							System.out.println("Saving...");
							model.refreshModel(contentText.getText());
						}
						else if(t == ButtonType.CLEAR){
							System.out.println("Clear");
							clearAll();
						}
						else if(t == ButtonType.SAVEANDQUIT){
							System.out.println("Saved and quit");
							model.refreshModel(contentText.getText());
							instance.setVisible(false);
						}
						else if(t == ButtonType.QUIT){
							instance.setVisible(false);
							System.out.println("Quit");
						}
					}
					button.setPositionRelativeToParent(pos);
					lowLight();
				default:
					break;
				}
				return false;
			}
		});	
	}

	public UseCaseComponentPopUp(MTApplication mtApplication, UseCaseModel model) {
		super(mtApplication);

		// TODO Auto-generated constructor stub
		nameField = new MTRectangle(mtApplication,width,nameFieldHeight);

		instance = this;
		this.model = model;

		this.setFillColor(new MTColor(122, 142, 117, 255));
		this.setStrokeColor(new MTColor(56, 62, 52, 255));
		this.setStrokeWeight(1);

		contentText = new KeyboardTextField (mtApplication,width,nameFieldHeight,KeyboardTextField.type.classes, null,this);
		contentText.setFillColor(new MTColor(122, 142, 117, 255));
		contentText.setStrokeColor(new MTColor(164, 117, 91, 255));

		nameField.addChild(contentText);
		nameField.setFillColor(new MTColor(122, 142, 117, 255));
		nameField.setStrokeColor(new MTColor(164, 117, 91, 255));
		nameField.setStrokeWeight(3);

	
		this.addChild(nameField);
	

		nameField.setPickable(false);
	
		this.addCornerButtonListener(saveButton, ButtonType.SAVE);
		this.addCornerButtonListener(clearButton, ButtonType.CLEAR);
		this.addCornerButtonListener(saveAndQuitButton, ButtonType.SAVEANDQUIT);
		this.addCornerButtonListener(quitButton, ButtonType.QUIT);
	}

}
